package com.xr.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    private final Long currentLoginUserId = 0L;

    /**
     * 插入时的填充策略
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段开始自动填充[insert]...,[metaObject:{}]", metaObject.toString());
        this.setFieldValByName("createId", currentLoginUserId, metaObject);
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateId", currentLoginUserId, metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    /**
     * 更新时的填充策略
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段开始自动填充[update]...,[metaObject:{}]", metaObject.toString());
        this.setFieldValByName("updateId", currentLoginUserId, metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

}
